/******************************************************************************
	Copyright 2005-2007 R2@DevFx.NET 
	DevFx.NET is free software; you can redistribute it and/or modify
	it under the terms of the Lesser GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	DevFx.NET is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	Lesser GNU General Public License for more details.

	You should have received a copy of the Lesser GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
/*******************************************************************************/

using System;
using System.Data;
using System.Data.Common;

namespace HTB.DevFx.Security.Data
{
	internal class AdoHelper
	{
		public AdoHelper(DbProviderFactory providerFactory) {
			if(providerFactory == null) {
				throw new ArgumentNullException("providerFactory");
			}
			this.providerFactory = providerFactory;
		}

		private DbProviderFactory providerFactory;

		public DbParameter CreateParameter(string parameterName, DbType dbType, ParameterDirection direction, object parameterValue) {
			DbParameter parameter = this.CreateParameter(parameterName);
			parameter.DbType = dbType;
			parameter.Direction = direction;
			parameter.Value = parameterValue;
			return parameter;
		}

		public DbParameter CreateParameter(string parameterName, DbType dbType, ParameterDirection direction) {
			DbParameter parameter = this.CreateParameter(parameterName);
			parameter.DbType = dbType;
			parameter.Direction = direction;
			return parameter;
		}

		public DbParameter CreateParameter(string parameterName, DbType dbType) {
			return this.CreateParameter(parameterName, dbType, ParameterDirection.Input);
		}

		public DbParameter CreateParameter(string parameterName, DbType dbType, object parameterValue) {
			return this.CreateParameter(parameterName, dbType, ParameterDirection.Input, parameterValue);
		}

		public DbParameter CreateParameter(string parameterName) {
			DbParameter parameter = this.CreateParameter();
			parameter.ParameterName = parameter.SourceColumn = parameterName;
			return parameter;
		}

		public DbParameter CreateParameter(string parameterName, object parameterValue) {
			DbParameter parameter = this.CreateParameter();
			parameter.ParameterName = parameter.SourceColumn = parameterName;
			parameter.Value = parameterValue;
			return parameter;
		}

		public DbParameter CreateParameter() {
			return this.providerFactory.CreateParameter();
		}

		public DbCommand CreateCommand(CommandType commandType, string commandText, params DbParameter[] parameters) {
			DbCommand cmd = this.providerFactory.CreateCommand();
			cmd.CommandType = commandType;
			cmd.CommandText = commandText;
			cmd.Parameters.AddRange(parameters);
			return cmd;
		}

		public DbCommand CreateCommand(string commandText, params DbParameter[] parameters) {
			return this.CreateCommand(CommandType.Text, commandText, parameters);
		}
	}
}